<template>
  <div class="search">
    <form @submit.prevent="searching">
      <div class="searchWrap">
        <i class="icon"></i>
        <input
          class="ipt"
          type="text"
          @input="inputing"
          placeholder="搜索歌曲、歌手、专辑"
          v-model="iptValue"
        />
        <div class="clear" @click="clearIpt" v-if="showClear"></div>
      </div>
    </form>
    <component
      :is="componentName"
      @hotsearch="searchChange"
      @suggest="clickSuggest"
      :keywords="iptValue"
      @play="$emit('play', $event)"
    ></component>
    <HistorySearch
      v-if="componentName == 'HotSearch'"
      @historysearch="searchChange"
    ></HistorySearch>
  </div>
</template>

<script>
import HotSearch from "@/components/HotSearch";
import HistorySearch from "@/components/HistorySearch";
import SearchSuggest from "@/components/SearchSuggest";
import SearchResult from "@/components/SearchResult";
export default {
  name: "Search",
  data() {
    return {
      iptValue: "",
      searchingFlag: false,
    };
  },
  created() {
    let keyword = this.$route.params.keyword;
    if (keyword) {
      this.iptValue = keyword;
      this.searchingFlag = true;
      // console.log(keyword);
    }
  },
  components: {
    HotSearch,
    HistorySearch,
    SearchSuggest,
    SearchResult,
  },
  computed: {
    showClear() {
      return this.iptValue != "";
    },
    componentName() {
      return this.searchingFlag
        ? "SearchResult"
        : this.iptValue == ""
        ? "HotSearch"
        : "SearchSuggest";
    },
  },
  methods: {
    inputing() {
      this.searchingFlag = false;
    },
    clearIpt() {
      this.iptValue = "";
      this.searchingFlag = false;
    },
    searching() {
      this.searchingFlag = true;
    },
    clickSuggest(e) {
      this.iptValue = e;
      this.searchingFlag = true;
    },
    searchChange(e) {
      this.iptValue = e;
      this.searchingFlag = true;
    },
  },
};
</script>

<style lang="less" scoped>
form {
  padding: 15px 10px;
  background: #fcfcfd;
  .searchWrap {
    position: relative;
    width: 100%;
    height: 30px;
    padding: 0 30px;
    box-sizing: border-box;
    background: #fefefe;
    border-radius: 30px;
    border: 1px solid #5dcaa8;
    opacity: 0.7;
    .icon {
      width: 13px;
      height: 13px;
      background-image: url();
      position: absolute;
      left: 0;
      top: 50%;
      transform: translateY(-50%);
      margin: 0 8px;
      vertical-align: middle;
    }
    .ipt {
      width: 100%;
      height: 28px;
      line-height: 28px;
      background: rgba(0, 0, 0, 0);
      font-size: 14px;
      border: 0;
      &::-webkit-input-placeholder {
        font-size: 14px;
        color: #c9c9c9;
      }
    }
    .clear {
      position: absolute;
      right: 0;
      top: 0;
      width: 30px;
      height: 30px;
      background-image: url();
      background-size: 14px 14px;
      background-repeat: no-repeat;
      background-position: center;
      cursor: pointer;
    }
  }
  border-bottom: 1px solid rgba(0, 0, 0, 0.05);
}
</style>